﻿using System.Windows.Input;
using CoreMVVM.Commands;
using CoreMVVM.ViewModels;
using CoreMVVM.Services;
using CoreMVVM.Messenger;
using System;
using System.ComponentModel;

namespace WpfModelViewApplication2.ViewModels
{
    class Window1ViewModel : ViewModelBase
    {
        private DelegateCommand saveFileCommand;
        private ISaveFileService saveFileService = null;
        private string fileName;
        

        public Window1ViewModel()
        {
            FileOpened = "Select a File using the Open File Dialog on the Main Window \r\n The selected file name is sent through the mediator to this VM";
            ViewModelBase.ServiceProvider.RegisterService<ISaveFileService>(new SaveFileService());
            saveFileService = GetService<ISaveFileService>();

            GetService<MessageMediator>().Register(MessageSilo.FileSelected, (Action<String>)OnFileSelected);
        }

        private void OnFileSelected(String fileName)
        {
            FileOpened = fileName;
        }

        public string FileOpened
        {
            get { return fileName; }
            set
            {
                fileName = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("FileOpened");
            }
        }

        public ICommand SaveFileCommand
        {
            get
            {
                if (saveFileCommand == null)
                {
                    saveFileCommand = new DelegateCommand(SaveFile);
                }
                return saveFileCommand;
            }
        }

        private void SaveFile()
        {
            bool? result = saveFileService.ShowDialog(null);
            if (result.HasValue && result.Value)
            {
            }
        }

    }
}
